package com.shm.service.impl;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.List;

import com.ruoyi.common.core.domain.entity.Picture;
import com.ruoyi.common.utils.DateUtils;
import com.shm.mapper.PictureMapper;
import com.shm.service.IPictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;

import static com.ruoyi.common.utils.PageUtils.clearPage;
import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 图片Service业务层处理
 *
 * @author dam
 * @date 2023-07-10
 */
@Service
public class PictureServiceImpl implements IPictureService {
    @Autowired
    private PictureMapper pictureMapper;

    /**
     * 查询图片
     *
     * @param id 图片主键
     * @return 图片
     */
    @Override
    public Picture selectPictureById(Long id) {
        return pictureMapper.selectPictureById(id);
    }

    /**
     * 查询图片列表
     *
     * @param picture 图片
     * @return 图片
     */
    @Override
    public List<Picture> selectPictureListByItemIdListAndType(Picture picture) {
        return pictureMapper.selectPictureList(picture);
    }

    /**
     * 根据 itemIdList 和 图片类型 来共同查询数据
     *
     * @param itemIdList
     * @param type
     * @return
     */
    @Override
    public List<Picture> selectPictureListByItemIdListAndType(List<Long> itemIdList, Integer type) {
        return pictureMapper.selectPictureListByItemIdListAndType(itemIdList, type);
    }

    /**
     * 新增图片
     *
     * @param picture 图片
     * @return 结果
     */
    @Override
    public int insertPicture(Picture picture) {
        picture.setCreateTime(DateUtils.getNowDate());
        return pictureMapper.insertPicture(picture);
    }

    /**
     * 修改图片
     *
     * @param picture 图片
     * @return 结果
     */
    @Override
    public int updatePicture(Picture picture) {
        picture.setUpdateTime(DateUtils.getNowDate());
        return pictureMapper.updatePicture(picture);
    }

    /**
     * 批量删除图片
     *
     * @param ids 需要删除的图片主键
     * @return 结果
     */
    @Override
    public int deletePictureByIds(Long[] ids) {
        return pictureMapper.deletePictureByIds(ids);
    }

    /**
     * 删除图片信息
     *
     * @param id 图片主键
     * @return 结果
     */
    @Override
    public int deletePictureById(Long id) {
        return pictureMapper.deletePictureById(id);
    }

    @Override
    public void insertPictures(List<Picture> pictureList) {
        pictureMapper.insertPictures(pictureList);
    }

    @Override
    public void updatePictureSheetSetAspectRatio() {
        Picture picture = new Picture();
        picture.setType(0);
        // 取消使用分页
        clearPage();
        List<Picture> pictureList = pictureMapper.selectPictureList(picture);
        for (Picture picture1 : pictureList) {
            String address = picture1.getAddress();
            try {
                BufferedImage sourceImg = ImageIO.read(new URL(address));
                picture1.setAspectRatio(sourceImg.getHeight() * 1.0 / sourceImg.getWidth());
                pictureMapper.updatePicture(picture1);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public void deletePicturesByItem(Long itemId, int type) {
        pictureMapper.deletePicturesByItem(itemId, type);
    }
}
